<?php
    $name=isset($_POST['name'])?$_POST['name']:'';
    $password=isset($_POST['password'])?$_POST['password']:'';
    $confirmpsw=isset($_POST['confirmpsw'])?$_POST['confirmpsw']:'';
    $code=isset($_POST['code'])?$_POST['code']:'';
    $action= isset($_GET['action'])?$_GET['action']:'';
    try {
        $dsn = 'mysql:host=localhost;dbname=yunbbs;charset=utf8';
        $user = 'root';
        $psw = '123456';
        $pdo = new PDO($dsn, $user, $psw, array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            ));
    } catch (Exception $exc) {
        echo '数据库连接失败' . $exc->getTraceAsString();
    }
    
    switch ($action) {
        case 'regist':
            session_start();
            $code1=$_SESSION['verifyCode'];
            if ($code!=$code1) {
                die('验证码不正确');
            }
            if($password!=$confirmpsw){
                die('密码不一致');
            }
//        $stmt = $pdo->prepare('select * from yunbbs_users where name=?');
//        $stmt->bindParam(1, $name);
//        $stmt->execute();
//        if ($stmt->rowCount() > 0) {
//            echo '该账号已经被注册' . '<hr>';
//        } else {
//            $stmt =$pdo->prepare("insert into yunbbs_users (name,password) value (:name,:password);");
//            $stmt->execute($_POST);
//            if ($stmt->rowCount() > 0) {
//                echo '注册成功' . '<hr>';
//            } else {
//                echo '注册失败' . '<hr>';
//            }
//        }
        $sql="select * from yunbbs_users where name='{$name}'";
	$result=$pdo->query($sql);
	$isExist=false;
	foreach ($result as $key => $value) {
		$isExist=true;
		break;
	}
	if (!$isExist) {
                $password= md5($password);
		$sql="insert into yunbbs_users (name,password) value ('$name','$password')";
		$effect_row=$pdo->exec($sql);
		if ($effect_row>0) {
			echo "注册成功";
                        header("Refresh:2;url=loginSuccess.php");
                        exit();
		} else {
			echo "注册失败";
                        header("Refresh:2;url=regist.php");
                        exit();
		}
	} else {
		echo "该账号已被占用";
                header("Refresh:2;url=regist.php");
                exit();
	}
            break;
        case 'login':
            session_start();
            $code1=$_SESSION['verifyCode'];
            if ($code!=$code1) {
                echo '验证码不正确';
                header("Refresh:2;url=login.php");
                exit();
            }
            $sql="select * from yunbbs_users where name='{$name}'";
            $result=$pdo->query($sql);
            $isExist=false;
            foreach ($result as $key => $value) {
                $isExist=true;
            }
            if(!$isExist){
                    echo '该用户不存在';
                    header("Refresh:2;url=login.php");
                    exit();
                }
            $password= md5($password);
            $sql="select password from yunbbs_users where name='{$name}'";
            $result=$pdo->query($sql);
            foreach ($result as $key => $value) {
                if($password!=$value['password']){
                    echo '密码不正确';
                    header("Refresh:2;url=login.php");
                    exit();
                }
            }
            echo "亲爱的{$name},欢迎您回来!";
            header("Refresh:2;url=loginSuccess.php");
            exit();
                break;
        case 'setting':
            
                break;
        case 'findback':

                break;
        default:
            break;
    }


